var zUrl = getApp().globalData.zUrl;
Page({
  data: {
    isGet: false,
    sec: 60,
  },
  watchTel: function (e) {
   
      this.setData({
        tel: e.detail.value
      })
    
  },
  watchYz: function (e) {

    this.setData({
      yzm: e.detail.value
    })

  },
  getCode() {
    var self = this
    wx.request({
      url: zUrl+'/api/send_sms',
      data: {
        tel: self.data.tel
      },
      header:{
        'Cookie':'laravel_session=' + wx.getStorageSync('laravel_session'),
      },
      method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      //预防乱码的不用管
      success: function (res) {
        wx.hideLoading()
        if(res.data.code==200){
          self.setData({ isGet: true })
          var remain = 60;
          var time = setInterval(function () {
            if (remain == 1) {
              clearInterval(time)
              self.setData({
                sec: 60,
                isGet: false
              })
              return false
            }
            remain--;
            self.setData({
              sec: remain
            })
          }, 1000)
        }
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
        

      },
      fail: function (error) {

      },
      complete: function (res) {

      }
    });
  },
  finish(){
    var self = this
    wx.request({
      url: zUrl+'/api/bind_phone',
      data: {
        tel: self.data.tel,
        sms_code: self.data.yzm
      },
      header: {
        'Cookie': 'laravel_session=' + wx.getStorageSync('laravel_session'),
      },
      method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      //预防乱码的不用管
      success: function (res) {
        wx.hideLoading()

        if (res.data.code == '200') {
          wx.setStorageSync('token', res.data.data.token)
          wx.setStorageSync('adviser', res.data.data.adviser)
          wx.switchTab({
            url: '/pages/index/index'
          })

        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }


      },
      fail: function (error) {

      },
      complete: function (res) {

      }
    });

  }
})
